// pages/replyDeatils/replyDeatils.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    isSend: false,
    content: '',
    iptShow: false,
    isSend: false,
    keyheight: 0
  },
  bigimg: function (e) {
    var that = this
    var dataset = e.currentTarget.dataset;
    // that.toggle("refresh", false);
    wx.previewImage({
      current: dataset.url, // 当前显示图片的http链接
      urls: dataset.list ? dataset.list : [dataset.url] // 需要预览的图片http链接列表
    })
  },
  toUserCard: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/myCardNew/myCardNew?id=' + id + "&type=friend",
    })
  },
  // 删除评论
  delComment(e) {
    var that = this
    var dataset = e.currentTarget.dataset;
    if (that.data.type) {
      var url = 'orgmini/home/removeComment'
      var data = {
        "messageId": that.data.messageId,
        "id": dataset.id,
        "type": that.data.type
      }
    } else {
      var url = 'orgmini/notice/removeComment'
      var data = {
        "messageId": that.data.messageId,
        "id": dataset.id
      }
    }
    wx.showModal({
      title: '提示',
      content: '您确定要删除当前评论吗?',
      confirmColor: '#2168EB',
      success(res) {
        if (res.confirm) {
          app.req({
            url: url,
            data: data,
            success: function (res) {

              if (200 == res.code) {
                if (dataset.id == that.data.main[0].id) {
                  wx.navigateBack({})
                } else {
                  var list = that.data.list.filter(function (item) {
                    return item.id != dataset.id
                  })
                  that.toggle('list', list)
                }
                wx.showToast({
                  title: '删除成功'
                })
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              }
            }
          });
        }
      }
    })
  },
  list: function(append) {
    var that = this;
    if(that.data.type==1){
     var url = 'orgmini/home/commentDetail'
    }else{
      var url = 'orgmini/notice/detailList'
    }
    app.req({
      url: url,
      data: {
        id: that.data.id
      },
      success: function(res) {
        if (200 == res.code) {
          var list = res.data;
          var main = list.splice(0, 1)
          that.toggle("main", main);
          that.toggle("list", list);
          wx.setNavigationBarTitle({
            title: list.length + '条回复',
          })
        }
      }
    });
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn
    wx.navigateTo({
      url: url
    })
  },
  setMore(e) {
    var dataset = e.currentTarget.dataset;
    if (dataset.type == 'remark') {
      var index = dataset.index;
      var temp_str = 'list[' + index + '].isMore';
      this.setData({
        [temp_str]: true
      })
    } else {
      this.toggle("isMore", true);
    }
  },
  isorghide(e) {
    var dataset = e.currentTarget.dataset;
    if (dataset.type == 'remark') {
      var index = dataset.index;
      var temp_str = 'list[' + index + '].isMore';
      this.setData({
        [temp_str]: false
      })
    } else {
      this.toggle("isMore", false)
    }
  },
  preventTouchMove: function (e) {
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("当前数据", options)
    this.toggle('id', options.id)
    this.toggle('messageId', options.messageId)
    if (options.type){
      this.toggle('type', options.type)
    }
    this.toggle('origin', options.origin)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    app.getUserInfo(function(userInfo) {
      that.toggle("uid", userInfo.id);
      that.list();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    app.getUserInfo(function(userInfo) {
      that.toggle("uid", userInfo.id);
      that.list();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 输入框显示
  toComment: function(e) {
    this.toggle('iptShow', true)
    var dataset = e.currentTarget.dataset;
    this.toggle('parentId', dataset.id)
    this.toggle('iptPlaceholder', dataset.name)
  },
  bindblur: function() {
    var that = this
    setTimeout(function() {
      that.toggle('iptShow', false)
    }, 300)
  },
  bindinput: function(e) {
    console.log("blur")
    this.toggle('content', e.detail.value)
    this.widthCheck(e.detail.value, 1000)
    if (this.data.content != '') {
      this.toggle('isSend', true)
    } else {
      this.toggle('isSend', false)
    }
  },
  bindfocus: function(e) {
    console.log(e.detail.height + 'keyboard')
    this.toggle('keyheight', e.detail.height)
    if (e.detail.value == '') {
      this.toggle('isSend', false)
    }
  },
  toSend: function() {
    var that = this;
    console.log(that.data.messageId)
    if (that.data.content != '' && !that.data.content.match(/^[ ]*$/)) {
      if (that.data.type) {
        var url = "orgmini/home/notice/addNewComment"
        var data = {
          "messageId": that.data.messageId,
          "content": that.data.content,
          "parentId": that.data.parentId,
          "type": that.data.type
        }
      } else {
        var url = "orgmini/notice/saveComment"
        var data = {
          "messageId": that.data.messageId,
          "content": that.data.content,
          "parentId": that.data.parentId
        }
      }
      app.req({
        url: url,
        method: "post",
        data: data,
        success: function(res) {
          console.log(res)
          that.list()
          wx.showToast({
            title: '评论成功',
            icon: 'success',
            duration: 2000
          })

        }
      });
      this.toggle('content', '')
    }
  },
  // 输入框长度字节限制
  widthCheck(str, maxLen) {
    var that = this;
    var w = 0;
    var tempCount = 0;
    //length 获取字数数，不区分汉子和英文 
    for (var i = 0; i < str.length; i++) {
      //charCodeAt()获取字符串中某一个字符的编码 
      var c = str.charCodeAt(i);
      //单字节加1 
      if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
        w++;
      } else {
        w += 2;
      }
      if (w > maxLen) {
        str = str.substr(0, i);
        that.toggle('content', str)
        break;
      }
    }
  }
})